package mx.com.mindbits.bandpay.fragments;


import java.util.List;

import mx.com.mindbits.bandpay.AddContacts;
import mx.com.mindbits.bandpay.R;
import mx.com.mindbits.bandpay.adapters.ContactAdapter;
import mx.com.mindbits.bandpay.business.ContactsBusiness;
import mx.com.mindbits.bandpay.dao.ContactsDAO;
import mx.com.mindbits.bandpay.utils.ConstantsValues;
import mx.com.mindbits.dao.contacts;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockFragment;

public class ContactsSettingsFragments extends SherlockFragment {
	private Button addContactos;
	private Button deleteContactos;
	private ListView listView;
	private List<contacts> _listaContactos;
	private ContactsBusiness _contactBusiness;
	private ContactsDAO _contactDAO;
	private contacts _contacts;
	private Context context;
	ContactAdapter _adapter;
		
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceBundle){
		View view = inflater.inflate(R.layout.fragment_contactos, container, false );
		context = view.getContext();
		_contactDAO = new ContactsDAO(context);
		
		
		addContactos = (Button) view.findViewById(R.id.bt_add_contact);
		deleteContactos = (Button) view.findViewById(R.id.bt_delete);
		listView = (ListView) view.findViewById(R.id.list_contacts);
		
		_listaContactos = _contactDAO.getAllContactsByUser();
		
		if(_listaContactos.size()>0){
			
			_adapter = new ContactAdapter(context,  _listaContactos);					
			listView.setAdapter(_adapter);					
			Log.i(ConstantsValues.TAG_DAO, "contactos" + _listaContactos.size());
			listView.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					
					Intent intent = new Intent(context, AddContacts.class);
					intent.putExtra("id_contact", _adapter.get_contactsA().get(arg2).getId_contact());

					Log.i(ConstantsValues.TAG_DAO, "long lista " + _adapter.get_contactsA().get(arg2).getId_contact());
					startActivity(intent);
				}
				
			});
			
		}else{
			deleteContactos.setEnabled(false);
		}
		
		
		addContactos.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, AddContacts.class);
				startActivity(intent);
				
			}});
		
		deleteContactos.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				
				Log.i(ConstantsValues.TAG_DAO, "long lista " + _adapter.get_contactsA().size());
				
				for(int i = 0; i<_adapter.get_contactsA().size(); i++){
					
					Log.i(ConstantsValues.TAG_DAO, "<<<<<<<<<<<<<<<elemento>>>> " +
					_adapter.get_contactsA().get(i).getContact_visible()
					+ " " +_adapter.get_contactsA().get(i).getContact_name() );
					
					if(!_adapter.get_contactsA().get(i).getContact_visible()){						
						
						Log.i(ConstantsValues.TAG_DAO, "<<<negado>>>> " +
						_adapter.get_contactsA().get(i).getContact_name() );
						_contactDAO.updateContacts(_adapter.get_contactsA().get(i));
							
						}
							
					
				}
				_listaContactos = _contactDAO.getAllContactsByUser();
				_adapter = new ContactAdapter(context,  _listaContactos);
				listView.setAdapter(_adapter);
				if(_listaContactos.size()==0){
					deleteContactos.setEnabled(false);
				}
			}
			
		});
		
		
		return view;
	}

}
